package manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import beans.ManagerPageBean;
import dtos.TechresourceDTO;
import dtos.UserDTO;
import utils.TechresourceDAORemote;
import utils.UserDAORemote;
import utils.Vals;

@ManagedBean(name = "printersBean")
@SessionScoped
public class PrinterStatusBean {
	@EJB
	TechresourceDAORemote tsr;

	@ManagedProperty("#{managerPageBean}")
	ManagerPageBean mpb;

	ResourceBundle bundle = ResourceBundle.getBundle(
			"languageSettings.language", FacesContext.getCurrentInstance()
					.getViewRoot().getLocale());

	/**
	 * @return the mpb
	 */
	public ManagerPageBean getMpb() {
		System.out.println(mpb.getPage());
		return mpb;
	}

	@EJB
    UserDAORemote ur;
	
	/**
	 * @param mpb
	 *            the mpb to set
	 */
	public void setMpb(ManagerPageBean mpb) {
		this.mpb = mpb;
	}

	private Date date;

	private List<TechresourceDTO> list;
	private List<Vals> listVals;
	private UserDTO u;
	private String checkip;

	private String message;
	private String pingYes = bundle
			.getString("Beans_manager_printerStatusBean_pingYes");
	private String pingNo = bundle
			.getString("Beans_manager_printerStatusBean_pingNo");

	@PostConstruct
	public void init() {
		listVals = new ArrayList<>();
		u = (UserDTO) FacesContext.getCurrentInstance().getExternalContext()
				.getSessionMap().get("loggedUser");
		//u=ur.getUser(1);
		refreshList();
		populateVals();
	}

	/**
	 * Method which returns a list of values
	 * 
	 * @return
	 */
	public List<Vals> getListVals() {
		return listVals;
	}

	/**
	 * Method which returns a list of values
	 * 
	 * @param listVals
	 */
	public void setListVals(List<Vals> listVals) {
		this.listVals = listVals;
		setMessage("");
	}

	public void populateVals() {
		listVals.clear();
		for (TechresourceDTO e : list) {
			listVals.add(new Vals(e));
		}
	}

	public List<TechresourceDTO> getList() {
		return list;
	}

	public void setList(List<TechresourceDTO> list) {
		this.setList(list);
	}

	public void refreshList() {
		this.list = tsr.getTechResourcesDTOPrinterByDomainId(u.getIdDomain());
	}

	public Date getDate() {
		this.date = new Date();
		return this.date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getCheckip() {
		return checkip;
	}

	public void setCheckip(String checkip) {
		this.checkip = checkip;
	}

	public String getResponse() {
		if (isReachableByPing(checkip) == true)
			return pingYes;
		return pingNo;

	}

	/**
	 * Method used to check if the parameter host is reachable by using the
	 * command ping
	 * 
	 * @param host
	 * @return
	 */
	public static boolean isReachableByPing(String host) {
		try {
			String cmd = "";
			if (System.getProperty("os.name").startsWith("Windows")) {
				// For Windows
				cmd = "ping -n 1 " + host;
			} else {
				// For Linux and OSX
				cmd = "ping -c 1 " + host;
			}

			Process myProcess = Runtime.getRuntime().exec(cmd);
			myProcess.waitFor();

			if (myProcess.exitValue() == 0) {

				return true;
			} else {

				return false;
			}

		} catch (Exception e) {

			e.printStackTrace();
			return false;
		}
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void getIpPrinterListener(ActionEvent event) {
		String ip = (String) event.getComponent().getAttributes().get("ip");
		if (isReachableByPing(ip) == true)
			setMessage(ip + " " + pingYes);
		else
			setMessage(ip + " " + pingNo);

	}

	public UserDTO getU() {
		return (UserDTO) FacesContext.getCurrentInstance().getExternalContext()
				.getSessionMap().get("loggedUser");
	}

	public void setU(UserDTO u) {
		this.u = u;
	}

	public String getPingYes() {
		return pingYes;
	}

	public void setPingYes(String pingYes) {
		this.pingYes = pingYes;
	}

	public String getPingNo() {
		return pingNo;
	}

	public void setPingNo(String pingNo) {
		this.pingNo = pingNo;
	}

}
